package com.xiaoyao.myframeapp.core.transformer;


import com.xiaoyao.myframeapp.FrameApplication;
import com.xiaoyao.myframeapp.R;
import com.xiaoyao.myframeapp.utils.NetworkUtil;
import com.xiaoyao.myframeapp.utils.ToastUtil;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author Administrator
 * @Author xiaoyao
 * @Email xiao80206040@qq.com
 * @Explain
 * @date 2018/7/3 0003
 */
public class RxSchedulers {

    public static <T> ObservableTransformer<T, T> compose() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> observable) {
                return observable
                        .subscribeOn(Schedulers.io())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(Disposable disposable) throws Exception {
                                if (!NetworkUtil.isNetworkAvailable(FrameApplication.getmContext())) {
                                    ToastUtil.setToast(R.string.toast_network_error);
                                }
                            }
                        })
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }
}

//        作者：Jaycee88
//        链接：https://www.jianshu.com/p/9674f6df910d
//        來源：简书
//        简书著作权归作者所有，任何形式的转载都请联系作者获得授权并注明出处。